package com.baobao.action;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
import net.sf.json.util.PropertyFilter;

import com.baobao.domain.Bbevent;
import com.baobao.domain.Notice;
import com.baobao.domain.School;
import com.baobao.service.BbeventService;
import com.baobao.service.NoticeService;
import com.baobao.service.SchoolService;
import com.baobao.service.StudentService;


@Controller
@RequestMapping("/publish")
public class PublishAction {

	@Autowired
	private NoticeService noticeService;
	
	@Autowired
	private SchoolService schoolService;
	
	/**
	 * 增加学校公告
	 */
	@RequestMapping(value="/add",produces = "application/json; charset=utf-8",method = {RequestMethod.POST})
	@ResponseBody
	public String AddNotice(HttpServletRequest request,
		@RequestParam(value="title",required=false) String title,
		@RequestParam(value="summary",required=false) String summary,
		@RequestParam(value="author",required=false) String author,
		@RequestParam(value="content",required=false) String content,
		@RequestParam(value="school_id",required=false) String school_id){
		
		Notice notice = new Notice();
		notice.setTitle(title);
		notice.setSummary(summary);
		notice.setTime(Calendar.getInstance());
		notice.setAuthor(author);
		notice.setContent(content);
		notice.setSchoolId(school_id);		
		noticeService.saveNotice(notice);
		return "SUCC";
	}
	
	/**
	 * 获取学校公告列表
	 */
	@RequestMapping(value="/get",produces = "application/json; charset=utf-8",method = {RequestMethod.GET})
	@ResponseBody
	public String GetNotice(HttpServletRequest request){
		
		JSONObject result = new JSONObject();
		JSONArray noticeList = new JSONArray();
				
		//从数据库中读取notice
		Set<Notice> notices = noticeService.loadNotices();
		for (Notice notice : notices) {
			JSONObject pieceNotice = JSONObject.fromObject(notice);
			
			//根据学校ID获取学校名字，并插入到JSON
			String schoolName = "";
			School school = schoolService.findSchoolByPrimaryKey(notice.getSchoolId());
			if(school == null)
			{
				System.out.println("学校ID："+notice.getSchoolId()+"不存在！");
			}
			else {
				schoolName = school.getName();
			}
			pieceNotice.put("school_name", schoolName);
			
			//将Calendar转化为YYYY-MM-DD，并插入到JSON
			String format ="yyyy-MM-dd";
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			String dateString = sdf.format(notice.getTime().getTime()); //calendar要先转换成date才能使用format转化为string
			pieceNotice.put("date", dateString);
			noticeList.add(pieceNotice);			
		}				
				
		result.put("success", true);
		result.put("message", noticeList);
		System.out.println(result.toString());
		return result.toString();
	}

}
